package com.cold.demo.web.util;

import org.springframework.util.StringUtils;

import java.util.Collection;
import java.util.Iterator;
import java.util.Random;
import java.util.regex.Pattern;

/**
 * @author zyc
 */
public class StringUtil extends StringUtils {
    /**
     * 空字符串常量
     */
    public static final String EMPTY_STR = "";

    /**
     * 所有的大写字母  65~90
     */
    private static final char[] UPPER_CASE_CHAR = new char[26];

    /**
     * 所有的小写字母  97~122
     */
    private static final char[] LOWER_CASE_CHAR = new char[26];

    /**
     * 所有的数字    48~57
     */
    private static final char[] NUMBER_CHAR = new char[10];

    /**
     * 常用的char数组，包含所有的数字和字母
     */
    private static final char[] COMMONLY_USED_CHAR = new char[UPPER_CASE_CHAR.length + LOWER_CASE_CHAR.length + NUMBER_CHAR.length];

    /**
     * 整数的正则
     */
    private final static String PATTERN_INT = "(-|\\+)[0-9]+";
    /**
     * 整数的 Pattern 对象
     */
    private final static Pattern P_INT = Pattern.compile(PATTERN_INT);
    /**
     * 小数的正则
     */
    private final static String PATTERN_DOUBLE = "(-|\\+)([1-9]+|0)\\.*[0-9]+";
    /**
     * 小数的 Pattern 对象
     */
    private final static Pattern P_DOUBLE = Pattern.compile(PATTERN_DOUBLE);

    static {
        // 大写字母
        int _A = 65;
        int _Z = 90;
        for (int i = _A; i <= _Z; i++) {
            UPPER_CASE_CHAR[i - _A] = (char) i;
        }
        // 小写字母
        int _a = 97;
        int _z = 122;
        for (int i = _a; i <= _z; i++) {
            LOWER_CASE_CHAR[i - _a] = (char) i;
        }
        // 0~9 的数字字符
        int _0 = 48;
        int _9 = 57;
        for (int i = _0; i <= _9; i++) {
            NUMBER_CHAR[i - _0] = (char) i;
        }
        System.arraycopy(UPPER_CASE_CHAR, 0, COMMONLY_USED_CHAR, 0, UPPER_CASE_CHAR.length);
        System.arraycopy(LOWER_CASE_CHAR, 0, COMMONLY_USED_CHAR, UPPER_CASE_CHAR.length, LOWER_CASE_CHAR.length);
        System.arraycopy(NUMBER_CHAR, 0, COMMONLY_USED_CHAR, UPPER_CASE_CHAR.length + LOWER_CASE_CHAR.length, NUMBER_CHAR.length);
    }


    public static char[] getUpperCaseCharByClone() {
        return UPPER_CASE_CHAR.clone();
    }

    public static char[] getLowerCaseCharByClone() {
        return LOWER_CASE_CHAR.clone();
    }

    public static char[] getNumberCharByClone() {
        return NUMBER_CHAR.clone();
    }

    public static char[] getAllCharByClone() {
        return COMMONLY_USED_CHAR.clone();
    }

    public static String getRandomInUpper(int length) {
        return getRandom(length, UPPER_CASE_CHAR);
    }

    public static String getRandomInLower(int length) {
        return getRandom(length, LOWER_CASE_CHAR);
    }

    public static String getRandomInNumber(int length) {
        return getRandom(length, NUMBER_CHAR);
    }

    public static String getRandomInAll(int length) {
        return getRandom(length, COMMONLY_USED_CHAR);
    }

    public static String getRandom(int length, char[] allChar) {
        if (allChar == null || allChar.length == 0) {
            return EMPTY_STR;
        }
        char[] chars = new char[length];
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            int index = random.nextInt(allChar.length);
            chars[i] = allChar[index];
        }
        return new String(chars);
    }

    public static boolean isEmpty(String str) {
        return str == null || str.length() == 0;
    }

    public static boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }

    public static <E> String join(Collection<E> coll, String split, String def) {
        if (coll == null || coll.size() == 0) {
            return def;
        }
        Iterator<E> iterator = coll.iterator();
        int i = 0;
        StringBuffer buffer = new StringBuffer();
        while (iterator.hasNext()) {
            E next = iterator.next();
            if (i++ == 0) {
                buffer.append(next);
            } else {
                buffer.append(split).append(next);
            }
        }
        return buffer.toString();
    }

    public static <E> String join(Collection<E> coll, String split) {
        return join(coll, split, "");
    }

    public static <E> String join(Collection<E> coll) {
        return join(coll, "、");
    }

    /**
     * 字符串乘法
     *
     * @param str    基础字符串
     * @param factor 因子
     * @return
     */
    public static String strMultiplication(String str, int factor) {
        if (str == null || factor <= 0) {
            return str;
        }
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < factor; i++) {
            builder.append(str);
        }
        return builder.toString();
    }

    public static boolean isNumber(String numberStr) {
        return isIntNumber(numberStr);
    }

    /**
     * 判断该字符串是否是一个 double 类型的字符串
     *
     * @param numberStr
     * @return
     */
    public static boolean isDoubleNumber(String numberStr) {
        return P_DOUBLE.matcher(numberStr).matches();
    }

    /**
     * 判断该字符串是否是一个 int 类型的字符串
     *
     * @param numberStr
     * @return
     */
    public static boolean isIntNumber(String numberStr) {
        return P_INT.matcher(numberStr).matches();
    }

}
